package org.grayrabbit.cloud.core.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.grayrabbit.cloud.core.Constants;
import org.grayrabbit.cloud.core.application.BizApplication;
import org.grayrabbit.cloud.core.biz.BizResult;
import org.grayrabbit.cloud.core.biz.ClientUtility;
import org.grayrabbit.cloud.core.biz.ObjectArgument;
import org.grayrabbit.cloud.core.command.CommandHandlerFactory;
import org.grayrabbit.cloud.core.module.ModuleConstructException;
import org.grayrabbit.cloud.core.module.ModuleException;
import org.grayrabbit.cloud.core.module.NoSuchModuleException;
import org.grayrabbit.cloud.core.rpc.Result;
import org.grayrabbit.cloud.utils.Console;
import org.grayrabbit.cloud.utils.logger.Output;


public class ClientConsole extends Console implements Output{
	public ClientConsole(String name,String url) throws ModuleConstructException{
		super(name);
		new BizApplication(url,false).run();
	}

	public void writeln(String arg) throws IOException {
		System.out.println(arg);
	}
	
	public static void main(String[] args) throws ModuleConstructException, IOException{
		Runtime.getRuntime().addShutdownHook(new Thread(){
    		public void run(){
    			System.out.println(" bye bye :-) ");
    		}
    	});
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		ClientConsole console = new ClientConsole("Clinet","client.xml");
		//ClientInvoker.register("client");
		println(module+ " Running ...");
		do {
            String command = in.readLine();            
            if(command != null && command.equals("quit")){
            	break;  
            }
            else if(command != null && command.equals("exit")){
            	break;  
            }else if(command != null && command.equals("sms")){
            	ObjectArgument argument = new ObjectArgument(Constants.MODULE_SMSCENTER);
            	argument.putServiceName("sendMessageApiImpl");
            	argument.putMethodName("send");
            	argument.putParam("recvNos", "15305712310");
            	argument.putParam("userId", "123456");
            	argument.putParam("userLoginName", "sys53");
            	argument.putParam("username", "沈永生");
            	argument.putParam("smsContext", "分布式短信接收情况。");
            	argument.putParam("systemCode", "163");
            	argument.putParam("smsType", "other");
            	argument.setObject(new Date());
            	 try {
					Result result = ClientUtility.getModule("channel").submit(argument);
					if(result instanceof BizResult){
						if(((BizResult) result).getResult()!=null)
							console.writeln(((BizResult) result).getResult().toString());
					}
				} catch (NoSuchModuleException e) {
					e.printStackTrace();
				} catch (ModuleException e) {
					e.printStackTrace();
				}
            }else if(command != null && command.equals("spring")){
            	ObjectArgument argument = new ObjectArgument(Constants.MODULE_COMMAND_BIZ_SPRING,"categoryServiceImpl","getCategory",new Object[]{"aaaaaaaa"});
            	try {
					Result result = ClientUtility.getModule("channel").submit(argument);
					if(result instanceof BizResult){
						console.writeln(((BizResult) result).getResult().toString());
					}
				} catch (NoSuchModuleException e) {
					e.printStackTrace();
				} catch (ModuleException e) {
					e.printStackTrace();
				}
            }
            else{
            	CommandHandlerFactory.getLocalCommandHandler(console,false).handler(command);
            }
            
        } while (!Thread.interrupted());
        System.exit(0);
	}
}
